package com.gaogzhen.datastructure.linear.linkedList;

public class LinkList<E> {
    private static class Node<E> {
        E item;
        Node<E> next;

        Node(E item, Node<E> next) {
            this.item = item;
            this.next = next;
        }
    }

    transient Node<E> head = new Node<>(null, null);
    transient int size;

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    private boolean isElementIndex(int index) {
        return index >= 0 && index < size;
    }

    public void checkElementIndex(int index) {
        if (!isElementIndex(index))
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

    private String outOfBoundsMsg(int index) {
        return "Index: " + index + ", Size: " + size;
    }

    public E get(int index) {
        checkElementIndex(index);
        return node(index).item;
    }

    private Node<E> node(int index) {
        Node<E> x = head;
        for (int i = 0; i <= index; i++) {
            x = x.next;
        }
        return x;
    }

    public int indexOf(Object o) {
        Node<E> x = head;
        if (o == null) {
            for (int i = 0; i < size; i++) {
                x = x.next;
                if (x.item == null)
                    return i;
            }
        }else {
            for (int i = 0; i < size; i++) {
                x = x.next;
                if (o.equals(x.item))
                    return i;
            }
        }
        return -1;
    }

    public E set(int index, E e) {
        checkElementIndex(index);
        Node<E> x = node(index);
        E oldValue = x.item;
        x.item = e;
        return oldValue;
    }

    public void add(E e) {
        Node<E> x = head;
        while (x.next != null)
            x = x.next;
        x.next = new Node<>(e, null);
        size++;
    }

    public void add(int index, E e) {
        checkElementIndex(index);

        Node<E> x = preNode(index);
        x.next = new Node<>(e, x.next);
        size++;
    }

    private Node<E> preNode(int index) {
        Node<E> x = head;
        for (int i = 0; i < index; i++)
            x = x.next;
        return x;
    }

    public boolean remove(Object o) {
        Node<E> x = head;
        if (o == null) {
            while (x.next != null) {
                if (x.next.item == null) {
                    Node<E> old = x.next;
                    x.next = x.next.next;
                    old.next = null;
                    size--;
                    return true;
                }
                x = x.next;
            }
        }else {
            while (x.next != null) {
                if (o.equals(x.next.item)) {
                    Node<E> old = x.next;
                    x.next = x.next.next;
                    old.next = null;
                    old.item = null;
                    size--;
                    return true;
                }
                x = x.next;
            }
        }
        return false;
    }

    public E remove(int index) {
        checkElementIndex(index);

        Node<E> prev = preNode(index);
        Node<E> old = prev.next;
        prev.next = old.next;
        old.next = null;
        E item = old.item;
        old.item = null;
        size--;
        return item;
    }

    public void clear() {
        Node<E> next = head.next;
        while (next != null) {
            Node<E> x = next;
            next = next.next;
            x.next = null;
            x.item = null;
        }
        size = 0;
    }

    @Override
    public String toString() {
        StringBuilder s = new StringBuilder();
        s.append("(");
        Node<E> x = head;
        for (int i = 0; i < size; i++) {
            x = x.next;
            if (i == size - 1) {
                s.append(x.item);
            } else {
                s.append(x.item).append(",");
            }
        }
        s.append(")");
        return s.toString();
    }
}
